﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using ControleRepresentanteSync.Application.Pedido;
using ControleRepresentanteSync.View.DynamicSync;

namespace ControleRepresentanteSync.View.Pedido
{
    class PedidoViewModel : DynamicSyncContentViewModel, IPedidoViewModel
    {
        public static readonly DependencyProperty NumeroProperty =
            DependencyProperty.Register("Numero", typeof(string), typeof(PedidoViewModel));
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(DateTime), typeof(PedidoViewModel));
        public static readonly DependencyProperty ClienteProperty =
            DependencyProperty.Register("Cliente", typeof(IClienteItem), typeof(PedidoViewModel), new PropertyMetadata(OnClienteChanged));
        public static readonly DependencyProperty CanChangeClienteProperty =
            DependencyProperty.Register("CanChangeCliente", typeof(bool), typeof(PedidoViewModel));
        public static readonly DependencyProperty VendedorProperty =
            DependencyProperty.Register("Vendedor", typeof(IVendedorItem), typeof(PedidoViewModel), new PropertyMetadata(OnVendedorChanged));
        public static readonly DependencyProperty TransportadoraProperty =
            DependencyProperty.Register("Transportadora", typeof(ITransportadoraItem), typeof(PedidoViewModel), new PropertyMetadata(OnTransportadoraChanged));
        public static readonly DependencyProperty FabricaProperty =
            DependencyProperty.Register("Fabrica", typeof(IFabricaItem), typeof(PedidoViewModel), new PropertyMetadata(OnFabricaChanged));
        public static readonly DependencyProperty ObservacaoProperty =
            DependencyProperty.Register("Observacao", typeof(string), typeof(PedidoViewModel));
        public static readonly DependencyProperty CondicaoPagamentoProperty =
            DependencyProperty.Register("CondicaoPagamento", typeof(string), typeof(PedidoViewModel));
        public static readonly DependencyProperty DescontoProperty =
            DependencyProperty.Register("Desconto", typeof(double), typeof(PedidoViewModel));
        public static readonly DependencyProperty PrazoEntregaProperty =
            DependencyProperty.Register("PrazoEntrega", typeof(string), typeof(PedidoViewModel));
        public static readonly DependencyProperty TotalMercadoriaProperty =
            DependencyProperty.Register("TotalMercadoria", typeof(double), typeof(PedidoViewModel));
        public static readonly DependencyProperty PecasProperty =
            DependencyProperty.Register("Pecas", typeof(int), typeof(PedidoViewModel));
        public static readonly DependencyProperty ValorDescontoProperty =
            DependencyProperty.Register("ValorDesconto", typeof(double), typeof(PedidoViewModel));
        public static readonly DependencyProperty ValorImpostoProperty =
            DependencyProperty.Register("ValorImposto", typeof(double), typeof(PedidoViewModel));
        public static readonly DependencyProperty TotalLiquidoProperty =
            DependencyProperty.Register("TotalLiquido", typeof(double), typeof(PedidoViewModel));
        public static readonly DependencyProperty CanChangeTransportadoraProperty =
            DependencyProperty.Register("CanChangeTransportadora", typeof(bool), typeof(PedidoViewModel));
        public static readonly DependencyProperty AttributeListProperty = 
            DependencyProperty.Register("AttributeList", typeof(IAttributeListViewModel), typeof(PedidoViewModel));

        private readonly ObservableCollection<IProdutoItem> m_Produtos;
        private readonly ObservableCollection<IClienteItem> m_Clientes;
        private readonly ObservableCollection<IFabricaItem> m_Fabricas;
        private readonly ObservableCollection<ITransportadoraItem> m_Transportadoras;
        private readonly ObservableCollection<IVendedorItem> m_Vendedores;

        public PedidoViewModel()
            : base("Pedido")
        {
            m_Produtos = new ObservableCollection<IProdutoItem>();
            m_Clientes = new ObservableCollection<IClienteItem>();
            m_Fabricas = new ObservableCollection<IFabricaItem>();
            m_Transportadoras = new ObservableCollection<ITransportadoraItem>();
            m_Vendedores = new ObservableCollection<IVendedorItem>();
        }

        private static void OnClienteChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OnChanged<IClienteItem>(d, e, (p, v) => ((PedidoViewModel)p).Cliente = v);
        }

        private static void OnFabricaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OnChanged<IFabricaItem>(d, e, (p, v) => ((PedidoViewModel)p).Fabrica = v);
        }

        private static void OnVendedorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OnChanged<IVendedorItem>(d, e, (p, v) => ((PedidoViewModel)p).Vendedor = v);
        }

        private static void OnTransportadoraChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OnChanged<ITransportadoraItem>(d, e, (p, v) => ((PedidoViewModel)p).Transportadora = v);
        }

        public string Numero
        {
            get { return (string)GetValue(NumeroProperty); }
            set { SetValue(NumeroProperty, value); }
        }

        public string Observacao
        {
            get { return (string)GetValue(ObservacaoProperty); }
            set { SetValue(ObservacaoProperty, value); }
        }

        public string CondicaoPagamento
        {
            get { return (string)GetValue(CondicaoPagamentoProperty); }
            set { SetValue(CondicaoPagamentoProperty, value); }
        }

        public double Desconto
        {
            get { return (double)GetValue(DescontoProperty); }
            set { SetValue(DescontoProperty, value); }
        }

        public string PrazoEntrega
        {
            get { return (string)GetValue(PrazoEntregaProperty); }
            set { SetValue(PrazoEntregaProperty, value); }
        }

        public DateTime Data
        {
            get { return (DateTime)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        public IClienteItem Cliente
        {
            get { return (IClienteItem)GetValue(ClienteProperty); }
            set { SetValue(ClienteProperty, value); }
        }

        public bool CanChangeCliente
        {
            get { return (bool)GetValue(CanChangeClienteProperty); }
            set { SetValue(CanChangeClienteProperty, value); }
        }

        public IVendedorItem Vendedor
        {
            get { return (IVendedorItem)GetValue(VendedorProperty); }
            set { SetValue(VendedorProperty, value); }
        }

        public ITransportadoraItem Transportadora
        {
            get { return (ITransportadoraItem)GetValue(TransportadoraProperty); }
            set { SetValue(TransportadoraProperty, value); }
        }

        public IFabricaItem Fabrica
        {
            get { return (IFabricaItem)GetValue(FabricaProperty); }
            set { SetValue(FabricaProperty, value); }
        }

        public double TotalMercadoria
        {
            get { return (double)GetValue(TotalMercadoriaProperty); }
            set { SetValue(TotalMercadoriaProperty, value); }
        }

        public int Pecas
        {
            get { return (int)GetValue(PecasProperty); }
            set { SetValue(PecasProperty, value); }
        }

        public double ValorImposto
        {
            get { return (double)GetValue(ValorImpostoProperty); }
            set { SetValue(ValorImpostoProperty, value); }
        }

        public double ValorDesconto
        {
            get { return (double)GetValue(ValorDescontoProperty); }
            set { SetValue(ValorDescontoProperty, value); }
        }

        public double TotalLiquido
        {
            get { return (double)GetValue(TotalLiquidoProperty); }
            set { SetValue(TotalLiquidoProperty, value); }
        }

        public bool CanChangeTransportadora
        {
            get { return (bool)GetValue(CanChangeTransportadoraProperty); }
            set { SetValue(CanChangeTransportadoraProperty, value); }
        }

        public IList<IClienteItem> Clientes { get { return m_Clientes; } }
        public IList<IFabricaItem> Fabricas { get { return m_Fabricas; } }
        public IList<ITransportadoraItem> Transportadoras { get { return m_Transportadoras; } }
        public IList<IProdutoItem> Produtos { get { return m_Produtos; } }
        public IList<IVendedorItem> Vendedores { get { return m_Vendedores; } }

        public IAttributeListViewModel AttributeList
        {
            get { return (IAttributeListViewModel) GetValue(AttributeListProperty); }
            set { SetValue(AttributeListProperty, value); }
        }
    }
}
